Python: divisors of a number [closed]
Posted
by kame
on Stack Overflow
See other posts from Stack Overflow
or by kame
Published on 2010-05-03T20:39:51Z
Indexed on
2010/05/03
21:28 UTC
Read the original article
Hit count: 1014
Possible Duplicate:
What is the best way to get all the divisors of a number?
The most part of this code was written by an other programmer, but I cant run his code. Please show me where the mistake is. I was searching a long time. I get the error 'NoneType' object is not iterable (in divisorGen(n)).
from __future__ import division
#calculate the divisors
#this is fast-working-code from:
#http://stackoverflow.com/questions/171765/what-is-the-best-way-to-get-all-the-divisors-of-a-number
def factorGenerator(n):
for x in range(1,n):
n = n * 1.0
r = n / x
if r % 1 == 0:
yield x # edited
def divisorGen(n):
factors = list(factorGenerator(n))
nfactors = len(factors)
f = [0] * nfactors
while True:
yield reduce(lambda x, y: x*y, [factors[x][0]**f[x] for x in range(nfactors)], 1)
i = 0
while True:
f[i] += 1
if f[i] <= factors[i][1]:
break
f[i] = 0
i += 1
if i >= nfactors:
return
for n in range(100):
for i in divisorGen(n):
print i
© Stack Overflow or respective owner